package com.qcs.sleep.warn;

import android.content.Intent;

import com.qcs.sleep.OnWarnListener;

/**
 * Created by chshqiang on 13-10-17.
 * 护眼模式
 */
public class EyeWarnAdapter implements WarnAdapter {
    private int useEyeTime = 0; // 用眼时间
    private boolean isScreenLocked = false;

    private OnWarnListener mListener;

    @Override
    public void run() {
        if (isScreenLocked) {
            if (0 != useEyeTime)
                useEyeTime = 3; // 护眼模式下，黑屏对护眼影响力很大
        } else {
            useEyeTime++;

            if (45 * 60 <= useEyeTime) { // 单位时间里用时间最长粒度
                if (null != mListener) {
                    mListener.onWarn(10, OnWarnListener.FLAG_TOAST, null);
                    useEyeTime = 0;
                }
            }
        }
    }

    @Override
    public void onReceive(String action) {
        if (Intent.ACTION_SCREEN_OFF.equals(action)) {
            isScreenLocked = true;
        } else if (Intent.ACTION_SCREEN_ON.equals(action)) {
            isScreenLocked = false;
        }
    }

    @Override
    public void setOnWarnListener(OnWarnListener listener) {
        mListener = listener;
    }
}
